//
//  ShowDetalImageViewController.m
//  WULIHH
//
//  Created by tang on 16/4/5.
//  Copyright © 2016年 tang. All rights reserved.
//

#import "ShowDetalImageViewController.h"
#import "Header.h"
#import "WuLiUserWorkReauest.h"
#import "UserInfoModel.h"
#import "WuLiRequestCommitAPI.h"
#import "WuLiUserWorkReauest.h"


#import "LoginViewViewController.h"


#import "DetalWorkTableViewCell.h"
#import "WorkDetalFooterView.h"

#import "DetailsTableView.h"
#import "TextFiledView.h"
#import "ThumbView.h"

#import "Header.h"

#import "ShareView.h"
@interface ShowDetalImageViewController ()<UITableViewDataSource, UITableViewDelegate, WorkDetalFooterViewDelegate, ThumbViewDelegate, ShareDelegate>
{
    UILabel *titleLable;
    NSDictionary *userInfoDic;
    
    UITableView *mainTableView;
    NSDictionary *imageInfoDic;
    NSArray *cellArray;
    WorkDetalFooterView *toolBarView;
    
    TextFiledView *textView;
    DetailsTableView *tableviews;
    ThumbView *thumbview;
    LoginViewViewController *loginView;
    
    UIView *fotterView;
    UIButton *morebtn;
//    UIView *bottomViews;
    ShareView *_share;
   
}

@end

@implementation ShowDetalImageViewController

- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    //设置底色
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WithView, 64.0)];
    [self.view addSubview:backView];
    [backView setBackgroundColor:uicolor(253, 202,47)];
    
    UIImage *image = nil;
    image = [UIImage imageNamed:@"Detalback.png"];
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 32, 10, 18)];
    [backButton setBackgroundImage:image forState:UIControlStateNormal];
    [self.view addSubview:backButton];
    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(backButton.frame)+6, 31.0, 50.0, 20.0)];
    [lable setText:@"返回"];
    [lable setFont:[UIFont systemFontOfSize:15.0]];
    [lable setTextAlignment:NSTextAlignmentLeft];
    [self.view addSubview:lable];
    
    UIButton *back = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
    [self.view addSubview:back];
    [back addTarget:self action:@selector(popTOVIEW) forControlEvents:UIControlEventTouchUpInside];
    [WuLiUserWorkReauest requestUserWork:_IDString];
    [WuLiRequestCommitAPI requestWorkCommit:_IDString];
    
    UILabel *titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(WithView /2.0 - 75, 25, 150, 30)];
//    [titlelabel setBackgroundColor:[UIColor redColor]];
    [titlelabel setTextAlignment:NSTextAlignmentCenter];
    [titlelabel setText:_textStr];
//    [titlelabel setFont:[UIFont systemFontOfSize:17.0]];
    
    [self.view addSubview:titlelabel];
    
    UILabel *moreLabel = [[UILabel alloc]initWithFrame:CGRectMake(WithView - 50, 28.0, 50, 30)];
    [moreLabel setText:@"更多"];
    [moreLabel setTextAlignment:NSTextAlignmentCenter];
    [moreLabel setFont:[UIFont systemFontOfSize:15.0]];
    [self.view addSubview:moreLabel];
    
    morebtn = [[UIButton alloc] initWithFrame:CGRectMake(WithView - 70, 28.0, 70, 70)];
//    [morebtn setBackgroundColor:[UIColor redColor]];
    [morebtn addTarget:self action:@selector(MoreEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:morebtn];
    
    thumbview = [[ThumbView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 55)];
    
    mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64.0, WithView, heightView-64.0-48)];
    [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:mainTableView];
    
    toolBarView = [[WorkDetalFooterView alloc] initWithFrame:CGRectMake(0, heightView-40.0, WithView, 30.0)];
    [self.view addSubview:toolBarView];
    
    textView = [[TextFiledView alloc] initWithFrame:CGRectMake(0, heightView - 48.0, WithView, 48.0)];
//    [self.view addSubview:textView];
    
    
    fotterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WithView, 55.0)];
    [fotterView addSubview:thumbview];
    [mainTableView setTableFooterView:thumbview];
    
    tableviews = [[DetailsTableView alloc] initWithFrame:CGRectMake(0, 0, WithView, 96+60*6)];
    
//    bottomViews = [[UIView alloc] initWithFrame:CGRectMake(0, heightView , WithView, 100)];
//    [bottomViews setBackgroundColor:[UIColor redColor]];
//    [self.view addSubview:bottomViews];
    
    _share = [[ShareView alloc] initWithFrame:CGRectMake(0, heightView, WithView, heightView)];
    [_share setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:_share];

}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [mainTableView setBackgroundColor:uicolor(237, 237, 237)];
    [mainTableView setDataSource:self];
    [mainTableView setDelegate:self];
    [mainTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    
    [thumbview setMThumbViewDelegate:self];
    [toolBarView setDelegate:self];
    [_share setDelegate:self];
    
    [self addSerNO];
    [textView setBackgroundColor:uicolor(254, 254, 254)];
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tabBarController setHidesBottomBarWhenPushed:YES];
    
    
}


- (void)addSerNO
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getWorkDetalGetBack:) name:zuopingxiangqing object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getFavWorkDetalGetBack:) name:shoucangzuopin object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(workCommitGetBack:) name:zuopinpinglin object:nil];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//  更多按钮
- (void)MoreEvent:(UIButton *)sender{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        CGRect frame;
        frame = _share.frame;
        CGFloat delta = frame.origin.y ;
        frame.origin.y = MAX(-heightView, frame.origin.y - delta);
        _share.frame = frame;
    }];
    UIButton *Button = sender;
    if (Button.selected == YES) {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect frame;
            frame = _share.frame;
            CGFloat delta = frame.origin.y;
            frame.origin.y = MAX(heightView, frame.origin.y - delta);
            _share.frame = frame;
        }];
    }
    Button.selected = !sender.selected;
}






/*返回*/
- (void)popTOVIEW
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)TouchOfShrinkDown{
   
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame;
        frame = _share.frame;
        CGFloat delta = frame.origin.y;
        frame.origin.y = MAX(heightView, frame.origin.y - delta);
        _share.frame = frame;
    }];

}



/*作品详情数据*/
- (void)getWorkDetalGetBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
//    NSLog(@"dic === %@",dic);
    [thumbview imageUrlStr:[[[dic valueForKey:@"data"]valueForKey:@"user"]valueForKey:@"iconUrl"]];
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        imageInfoDic = [dic valueForKey:@"data"];
//        thumbview.imageData = [NSString stringWithFormat:@"%@",[imageInfoDic[@"user"]valueForKey:@"iconUrl"]];
        if ([[imageInfoDic valueForKey:@"isLiked"] intValue] == 0)
        {
            [thumbview setSelect:NO];
        }
        else
        {
            [thumbview setSelect:YES];
        }
        
        cellArray = [imageInfoDic valueForKey:@"images"];
//        NSLog(@"%@",cellArray);
        [mainTableView reloadData];
    }
    else
    {
        
    }
}
- (void)working{






}


/*作品评论*/
- (void)workCommitGetBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
    
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
//        NSLog(@"%@", dic);
        NSArray *array = [dic valueForKey:@"data"];
        if ([array count] == 0)
        {
//            [tableviews setHidden:YES];
        }
        else
        {
            if (array.count > 0 || array.count < 6)
            {
                [tableviews setFrame:CGRectMake(0, 0, WithView, 96+60+array.count)];
                [tableviews setCommitInfo:dic];
                [mainTableView setTableFooterView:tableviews];
            }
        }
    }
    else
    {
//        [tableviews setHidden:YES];
    }
}



#pragma mark---dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return cellArray.count;
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellInden = @"cell";
    DetalWorkTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInden];
    if (!cell) {
        cell = [[DetalWorkTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInden];
    }
//    [cell setDelegate:self];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    [cell setImageUrl:[cellArray[indexPath.row] valueForKey:@"imageUrl"]];
    [cell WithHeight:[[cellArray[indexPath.row] valueForKey:@"height"] floatValue] withWidth:[[cellArray[indexPath.row] valueForKey:@"width"] floatValue]];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    UIButton *backgroundButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, cell.bounds.size.width, cell.bounds.size.height)];
    [backgroundButton addTarget:self action:@selector(Touch) forControlEvents:UIControlEventTouchUpInside];
    [cell addSubview:backgroundButton];
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
//    NSLog(@"%f", heightView);
    if (scrollView.contentOffset.y > scrollView.contentSize.height-scrollView.frame.size.height)
    {
        NSLog(@"chuxian");
    }
    else
    {
        NSLog(@"xiaoshi");
    }
}
- (void)Touch{
    
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame;
        frame = _share.frame;
        CGFloat delta = frame.origin.y;
        frame.origin.y = MAX(heightView, frame.origin.y - delta);
        _share.frame = frame;
    }];

}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [UIView animateWithDuration:0.3 animations:^{
        CGRect frame;
        frame = _share.frame;
        CGFloat delta = frame.origin.y;
        frame.origin.y = MAX(heightView, frame.origin.y - delta);
        _share.frame = frame;
    }];

}
//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    if (section == 0) {
//        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 55)];
//        
//        
//        [view addSubview:thumbview];
//        return view;
//    }
//    return nil;
//    
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    if (section == 0) {
//        return 55;
//    }
//    return 0;
//}





#pragma mark----对作品点赞
- (void)removeConnect
{
    NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:userLogin];
    //没有登录
    if ([string isEqualToString:@"no"])
    {
    }
    else
    {
        [thumbview setSelect:NO];
        [WuLiUserWorkReauest requestDeleteGoodWork:_IDString];
    }
}

- (void)connectWork
{
    NSString *string = [[NSUserDefaults standardUserDefaults] valueForKey:userLogin];
    //没有登录
    if ([string isEqualToString:@"no"])
    {
        loginView = [[LoginViewViewController alloc] init];
        [self.view addSubview:loginView.view];
        
    }
    else
    {
//        NSLog(@"2222222222");
        [thumbview setSelect:NO];
        [WuLiUserWorkReauest requestGoodWork:_IDString];
//        thumbview.imageData = [NSString stringWithFormat:@"%@",[imageInfoDic[@"user"]valueForKey:@"iconUrl"]];
    }

}


#pragma mark----对作品操作
- (void)favStar
{
    [WuLiUserWorkReauest requestWorkCollect:_IDString];
    //    NSLog(@"33333333");
}


- (void)getFavWorkDetalGetBack:(NSNotification *)notion
{
    NSDictionary *dic = [notion userInfo];
    
    if ([[dic valueForKey:@"success"] intValue] == 1)
    {
        
    }
    else
    {
        
    }
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
